/********************************************************/
/*	Copyright (C) 2016 Gong Li Bin			*/
/*	Project:	GlbAdpTanx-1.0.0		*/
/*	Author:		gong_libin			*/
/*	Date:		2016_06_01			*/
/*	File:		GlbAdpRtb.cpp			*/
/********************************************************/

#include "GlbPro.h"
#include "GlbUdp.h"
#include "GlbTime.h"
#include "GlbRtbDef.h"

#define	GLB_AR_KVS			128

bool g_bPrint = false;

int GlbAdpRtbCore(GlbNet::CGlbPro& CPro, UCHAR* puszSend)
{
	USHORT usCount = 0;
	GlbNet::GLBPROKV_S stKv[GLB_AR_KVS] = {{'\0', 0, NULL}};
	char szSnippet[] = "<iframe width=\"468\" height=\"60\" frameborder=\"0\" scrolling=\"no\" src=\"http://geo.material.com/flash/media.html?p=\%SETTLE_PRICE%&bid=7fa9844a6d8226d634ce0fbda09d0e99&info=MDE6MjAxMy0wOS0xMSAxNToyMzoxNgkwMjowMQkwMzoxCTA0OjdmYTk4NDRhNmQ4MjI2ZDYzNGNlMGZiZGEwOWQwZTk5CTA1Ok5OZmxlTlJmNEcvRDlWRXZldi8zM1htaAkwNzo0MgkwODoxODAJMDk6MAkxMDptbV8xMjNfNDU2Xzc4OQkxMTozCTEyOjMtMjg1LTIwMw&tanxclick=\%CLICK_URL%\"></iframe>";

	// required uint32 id = 1; 
	stKv[usCount].m_ucKey = GLB_RD_ADID;
	stKv[usCount].m_puszValue = (UCHAR*)"0";
	stKv[usCount].m_iLength = strlen((char*)stKv[usCount].m_puszValue);
	if (true == g_bPrint) {
		GLB_PRINT("=== Response ===\n");
		GLB_PRINT("id: %s\n", (char*)stKv[usCount].m_puszValue);
	}
	usCount += 1;

	// required uint32 max_cpm_price = 2;
	stKv[usCount].m_ucKey = GLB_RD_ADMX;
	stKv[usCount].m_puszValue = (UCHAR*)"180";
	stKv[usCount].m_iLength = strlen((char*)stKv[usCount].m_puszValue);
	if (true == g_bPrint) {
		GLB_PRINT("max_cpm_price: %s\n", (char*)stKv[usCount].m_puszValue);
	}
	usCount += 1;

	// optional uint32 ad_bid_count_idx = 3;
	stKv[usCount].m_ucKey = GLB_RD_ADBI;
	stKv[usCount].m_puszValue = (UCHAR*)"0";
	stKv[usCount].m_iLength = strlen((char*)stKv[usCount].m_puszValue);
	if (true == g_bPrint) {
		GLB_PRINT("ad_bid_count_idx: %s\n", (char*)stKv[usCount].m_puszValue);
	}
	usCount += 1;

	// optional string html_snippet = 4;
	stKv[usCount].m_ucKey = GLB_RD_ADHS;
	stKv[usCount].m_puszValue = (UCHAR*)szSnippet;
	stKv[usCount].m_iLength = strlen((char*)stKv[usCount].m_puszValue);
	if (true == g_bPrint) {
		GLB_PRINT("ihtml_snippet: %s\n", (char*)stKv[usCount].m_puszValue);
	}
	usCount += 1;

	// repeated string click_through_url = 5;
	stKv[usCount].m_ucKey = GLB_RD_ADTU;
	stKv[usCount].m_puszValue = (UCHAR*)"http://www.geotmt.com";
	stKv[usCount].m_iLength = strlen((char*)stKv[usCount].m_puszValue);
	if (true == g_bPrint) {
		GLB_PRINT("click_through_url: %s\n", (char*)stKv[usCount].m_puszValue);
	}
	usCount += 1;

	// repeated int32 category = 6;
	stKv[usCount].m_ucKey = GLB_RD_WCAT;
	stKv[usCount].m_puszValue = (UCHAR*)"28";
	stKv[usCount].m_iLength = strlen((char*)stKv[usCount].m_puszValue);
	if (true == g_bPrint) {
		GLB_PRINT("category: %s\n", (char*)stKv[usCount].m_puszValue);
	}
	usCount += 1;

	// repeated int32 creative_type = 7;
	stKv[usCount].m_ucKey = GLB_RD_ADCT;
	stKv[usCount].m_puszValue = (UCHAR*)"3";
	stKv[usCount].m_iLength = strlen((char*)stKv[usCount].m_puszValue);
	if (true == g_bPrint) {
		GLB_PRINT("creative_type: %s\n", (char*)stKv[usCount].m_puszValue);
	}
	usCount += 1;

	// optional string network_guid = 8;
	/*stKv[usCount].m_ucKey = GLB_RD_ADNG;
	stKv[usCount].m_puszValue = (UCHAR*)"";
	stKv[usCount].m_iLength = strlen((char*)stKv[usCount].m_puszValue);
	if (true == g_bPrint) {
		GLB_PRINT("network_guid: %s\n", (char*)stKv[usCount].m_puszValue);
	}
	usCount += 1;*/

	// optional string extend_data = 9;
	/*stKv[usCount].m_puszValue = (UCHAR*)"";
	stKv[usCount].m_ucKey = GLB_RD_ADED;
	stKv[usCount].m_iLength = strlen((char*)stKv[usCount].m_puszValue);
	if (true == g_bPrint) {
		GLB_PRINT("extend_data: %s\n", (char*)stKv[usCount].m_puszValue);
	}
	usCount += 1;*/

	// repeated string destination_url = 10;
	stKv[usCount].m_ucKey = GLB_RD_ADDU;
	stKv[usCount].m_puszValue = (UCHAR*)"http://www.geotmt.com";
	stKv[usCount].m_iLength = strlen((char*)stKv[usCount].m_puszValue);
	if (true == g_bPrint) {
		GLB_PRINT("destination_url: %s\n", (char*)stKv[usCount].m_puszValue);
	}
	usCount += 1;

	// optional string creative_id = 11;
	stKv[usCount].m_ucKey = GLB_RD_ADCI;
	stKv[usCount].m_puszValue = (UCHAR*)"3-285-203";
	stKv[usCount].m_iLength = strlen((char*)stKv[usCount].m_puszValue);
	if (true == g_bPrint) {
		GLB_PRINT("creative_id: %s\n", (char*)stKv[usCount].m_puszValue);
	}
	usCount += 1;

	CPro.GlbProPutBody(NULL);
	CPro.GlbProPutTotal(usCount);
	CPro.GlbProPutVerb(GLB_RD_RES);
	CPro.GlbProPutLength(GlbProKvOutput(stKv, usCount, puszSend + GLB_PRO_HEAD, GLB_PACKET));
	CPro.GlbProCreate(puszSend, GLB_PRO_HEAD);

	return GLB_PRO_HEAD + CPro.GlbProGetLength();
}

int main(int argc, char* argv[])
{
	int iCount = 1;
	int iLength = 0;
	GlbNet::CGlbPro CPro;
	GlbNet::CGlbUdp CUdp;
	GlbCls::CGlbTime CTime;
	char szPort[GLB_BYTE16] = "20211";
	UCHAR uszRecv[GLB_PACKET] = { 0 };
	UCHAR uszSend[GLB_PACKET] = { 0 };
	GlbNet::GLBPROKV_S stKv[GLB_AR_KVS] = {{'\0', 0, NULL}};

	if (argc > 1) {
		g_bPrint = true;
	}

	/* init */
	if (GLB_SUCCESS == CUdp.GlbNetInitRecv(NULL, szPort)) {
		/* recv */
		while ((iLength = CUdp.GlbNetRecv(uszRecv, GLB_PACKET)) > 0) {
			/* on */
			if (true == g_bPrint) {
				CTime.GlbTimeOn();
				GLB_PRINT("\nCount[%d]:\n=== Request ===\n", iCount ++);
			}

			/* head */
			CPro.GlbProParse(uszRecv, iLength);
			if (true == g_bPrint) {
				CPro.GlbProDisplay();
			}

			/* body */
			GlbProKvInput(stKv, CPro.GlbProGetTotal(), CPro.GlbProGetBody(), CPro.GlbProGetLength());
			if (true == g_bPrint) {
				GlbProKvDisplay(stKv, CPro.GlbProGetTotal());
			}

			/* core */
			iLength = GlbAdpRtbCore(CPro, uszSend);

			/* send */
			if (GLB_FAILURE == CUdp.GlbNetSend(uszSend, iLength)) {
				GLB_ERROR("Failed to GlbNetSend\n");
			}

			/* off */
			if (true == g_bPrint) {
				GLB_PRINT("Use time ");
				CTime.GlbTimeOff();
				CTime.GlbTimeDisplay();
			}
		}
		CUdp.GlbNetClose();
	}
	else {
		GLB_ERROR("Failed to GlbNetInitRecv\n");
	}

	return GLB_SUCCESS;
}
